{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Tce3stUlHN0L"
      },
      "source": [
        "##### Copyright 2020 The TensorFlow IO Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "tuOe1ymfHZPu"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qFdPvlXBOdUN"
      },
      "source": [
        "# Color Space Conversions"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MfBg1C5NB3X0"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://www.tensorflow.org/io/tutorials/colorspace\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/io/blob/master/docs/tutorials/colorspace.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/tensorflow/io/blob/master/docs/tutorials/colorspace.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
        "  </td>\n",
        "      <td>\n",
        "    <a href=\"https://storage.googleapis.com/tensorflow_docs/io/docs/tutorials/colorspace.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xHxb-dlhMIzW"
      },
      "source": [
        "## Overview\n",
        "\n",
        "In computer vision, the selected color space could have a significant the performance of the model. While `RGB` is the most common color space, in manay situations the model performs better when switching to alternative color spaces such as `YUV`, `YCbCr`, `XYZ (CIE)`, etc. \n",
        "\n",
        "The `tensorflow-io` package provides a list of color space conversions APIs that can be used to prepare and augment the image data."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MUXex9ctTuDB"
      },
      "source": [
        "## Setup"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "upgCc3gXybsA"
      },
      "source": [
        "### Install required Packages, and restart runtime"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "uUDYyMZRfkX4"
      },
      "outputs": [],
      "source": [
        "!pip install tensorflow-io"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VSkY6UAxODOq"
      },
      "source": [
        "### Download the sample image\n",
        "\n",
        "The image example used in this tutorial is a [cat in the snow](https://commons.wikimedia.org/wiki/File:Felis_catus-cat_on_snow.jpg), though it could be replaced by any JPEG images.\n",
        "\n",
        "The following will download the image and save to local disk as `sample.jpg`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "baeqVsglPQUI"
      },
      "outputs": [],
      "source": [
        "!curl -o sample.jpg -L https://storage.googleapis.com/download.tensorflow.org/example_images/320px-Felis_catus-cat_on_snow.jpg\n",
        "\n",
        "!ls -ls sample.jpg"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "J0ZKhA6s0Pjp"
      },
      "source": [
        "## Usage"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yZmI7l_GykcW"
      },
      "source": [
        "### Read Image File\n",
        "\n",
        "Read and decode the image into a `uint8` Tensor of shape `(213, 320, 3)`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "nS3eTBvjt-O5"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import tensorflow_io as tfio\n",
        "\n",
        "image = tf.image.decode_jpeg(tf.io.read_file('sample.jpg'))\n",
        "\n",
        "print(image.shape, image.dtype)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IGnbXuVnSo8T"
      },
      "source": [
        "The image can be displayed by:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "0rLbVxuFSvVO"
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "\n",
        "plt.figure()\n",
        "plt.imshow(image)\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "86qE8BPl5rcA"
      },
      "source": [
        "### Convert RGB to Grayscale\n",
        "\n",
        "An `RGB` image can be converted to `Grayscale` to reduce the channel from 3 to 1 with `tfio.experimental.color.rgb_to_grayscale`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "eEa0Z5U26Ep3"
      },
      "outputs": [],
      "source": [
        "grayscale = tfio.experimental.color.rgb_to_grayscale(image)\n",
        "\n",
        "print(grayscale.shape, grayscale.dtype)\n",
        "\n",
        "# use tf.squeeze to remove last channel for plt.imshow to display:\n",
        "plt.figure()\n",
        "plt.imshow(tf.squeeze(grayscale, axis=-1), cmap='gray')\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ineBzDeu-lTh"
      },
      "source": [
        "### Convert RGB to BGR\n",
        "\n",
        "Some image software and camera manufacturors might prefer `BGR`, which can be obtained through `tfio.experimental.color.rgb_to_bgr`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "LfZo0XaaAaeM"
      },
      "outputs": [],
      "source": [
        "bgr = tfio.experimental.color.rgb_to_bgr(image)\n",
        "\n",
        "print(bgr.shape, bgr.dtype)\n",
        "\n",
        "plt.figure()\n",
        "plt.imshow(bgr)\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nXd776xNIr_I"
      },
      "source": [
        "### Convert RGB to CIE XYZ\n",
        "\n",
        "`CIE XYZ` (or `CIE 1931 XYZ` is a common color space used in many image processing programs. The following is the conversion from RGB to `CIE XYZ` through `tfio.experimental.color.rgb_to_xyz`. Note `tfio.experimental.color.rgb_to_xyz` assumes floating point input in the range of `[0, 1]` so additional pre-processing is needed:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "kLEdfkkoK27A"
      },
      "outputs": [],
      "source": [
        "# convert to float32\n",
        "image_float32 = tf.cast(image, tf.float32) / 255.0\n",
        "\n",
        "xyz_float32 = tfio.experimental.color.rgb_to_xyz(image_float32)\n",
        "\n",
        "# convert back uint8\n",
        "xyz = tf.cast(xyz_float32 * 255.0, tf.uint8)\n",
        "\n",
        "print(xyz.shape, xyz.dtype)\n",
        "\n",
        "plt.figure()\n",
        "plt.imshow(xyz)\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7rhLvOSZB0k0"
      },
      "source": [
        "### Convert RGB to YCbCr\n",
        "\n",
        "Finally, `YCbCr` is the default color space in many video systems. Converting to `YCbCr` could be done through `tfio.experimental.color.rgb_to_ycbcr`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "UyFMBK-LDDnN"
      },
      "outputs": [],
      "source": [
        "ycbcr = tfio.experimental.color.rgb_to_ycbcr(image)\n",
        "\n",
        "print(ycbcr.shape, ycbcr.dtype)\n",
        "\n",
        "plt.figure()\n",
        "plt.imshow(ycbcr, cmap='gray')\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VuL8rrnhwab4"
      },
      "source": [
        "What is more interesting, though, is that `YCbCr` could be decomposed into `Y'` (luma), `Cb` (blue-difference chroma), and `Cr` (red-difference chroma) components with each component carry perceptually meaningful information:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "wvur-gtyxjwG"
      },
      "outputs": [],
      "source": [
        "y, cb, cr = ycbcr[:,:,0], ycbcr[:,:,1], ycbcr[:,:,2]\n",
        "\n",
        "# Y' component\n",
        "plt.figure()\n",
        "plt.imshow(y, cmap='gray')\n",
        "plt.axis('off')\n",
        "plt.show()\n",
        "\n",
        "# Cb component\n",
        "plt.figure()\n",
        "plt.imshow(cb, cmap='gray')\n",
        "plt.axis('off')\n",
        "plt.show()\n",
        "\n",
        "# Cr component\n",
        "plt.figure()\n",
        "plt.imshow(cr, cmap='gray')\n",
        "plt.axis('off')\n",
        "plt.show()"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [
        "Tce3stUlHN0L"
      ],
      "name": "colorspace.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
